package weaver.interfaces.jjy.duss.hr;

import com.sap.mw.jco.JCO;
import com.weaver.general.BaseBean;
import weaver.interfaces.jjy.duss.util.SAPComMehtod;
import weaver.interfaces.jjy.duss.util.SAPConnPool;

import java.util.HashMap;
import java.util.Map;

/*
 *"促销员离职"接口，入参：
 *          JIEKNO      HR016
 *          IV_PERNR    10120370
 *          PS0000	PERNR	员工号	10120370
 *          PS0000	BEGDA	事件开始日期	20191201
 *          PS0000	MASSN	事件类型	Z9
 *          PS0000	MASSG	事件原因	91
 * */

public class HR016 {

    public Map execute(Map<String, String> map) {

        SAPConnPool SAPConn = new SAPConnPool();
        JCO.Client myConnection = SAPConn.getConnection();
        JCO.Function bapi = SAPConn.excuteBapi("ZFM_HR_PERNR");

        JCO.ParameterList input=bapi.getImportParameterList();//输入参数和结构处理
        //JCO.ParameterList inputtable= bapi.getTableParameterList();//输入表的处理

        //入参为字段
        input.setValue("HR016", "JIEKNO");                   //HR接口标识：离职
        input.setValue(map.get("IV_PERNR"), "IV_PERNR");

        //入参为结构
        JCO.Structure stu0=input.getStructure("PS0000");
        stu0.setValue(map.get("PERNR"), "PERNR");
        stu0.setValue(map.get("BEGDA"), "BEGDA");                 //事件日期
        stu0.setValue(map.get("MASSN"), "MASSN");                       //事件类型
        stu0.setValue(map.get("MASSG"), "MASSG");                       //事件原因

        JCO.Structure stu1 = input.getStructure("PS9011");
        stu1.setValue(map.get("ZHR_SSGYS"), "ZHR_SSGYS");            //所属供应商
        //执行函数
        myConnection.execute(bapi);
        JCO.ParameterList out = bapi.getExportParameterList();//输出参数和结构处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();//输出表的处理

        //返回处理结果以及日志
        Map<String, String> mapreturn = new HashMap<>();
        JCO.Structure stu = out.getStructure("EV_OUTPUT");
        mapreturn.put("flag",(String) stu.getValue("ZFLAG"));
        mapreturn.put("message",(String) stu.getValue("ZMESG"));
        if("E".equals(mapreturn.get("flag"))){
            //遍历
            BaseBean baseBean = new BaseBean();
            String structure = baseBean.getPropValue("SAPAllTable","structure");//所有结构
            String[] structures = structure.split(",");
            String table = baseBean.getPropValue("SAPAllTable","table");//所有表
            String[] tables = table.split(",");

            mapreturn = SAPComMehtod.SAPreturnS(structures, out, tables, Table00);
            baseBean.writeLog("SAP返回结果"+mapreturn.toString());
        }
        JCO.releaseClient(myConnection);
        return mapreturn;
    }

}
